package com.wing.sys.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.wing.common.CommonSpringJDBC;
import com.wing.common.util.PageBean;
import com.wing.sys.domain.Module;

@Repository
public class ModuleDao extends CommonSpringJDBC<Module, String> {
	static final Log logger = LogFactory.getLog(ModuleDao.class);
	
	public List<Map<String, Object>> findChildByID(String id, boolean ismenu, String userId) {
		String sql = "select t.* from sys_module t where t.parent_id=? order by order_num ";
		if(ismenu){
			sql = "select t.* from sys_module t where t.parent_id=? and ismenu=1 order by order_num";
		}
		if(!ismenu && userId !=null){
			sql = "select distinct m.* from sys_role_user ru,sys_role_module rm,sys_module m " +
				" where ((ru.userid='"+userId+"' and ru.roleid=rm.roleid and rm.moduleid = m.id) or m.ispub=1) and m.parent_id=?  " +
				" order by m.order_num";
		}
		if(ismenu && userId !=null){
			sql = "select distinct m.* from sys_role_user ru,sys_role_module rm,sys_module m " +
				" where ((ru.userid='"+userId+"' and ru.roleid=rm.roleid and rm.moduleid = m.id) or m.ispub=1)  and m.parent_id=? and m.ismenu=1 " +
				" order by m.order_num";
		}
		return super.getSimpleJdbcTemplate().queryForList(sql,id);
	}
	
	public PageBean findPageList(int currentPage, int pageSize, String moduleId) {
		String where= " parent_id='"+moduleId+"'  ";
		String sql1=" SELECT * FROM sys_module where "+where+" ORDER BY order_num ASC,id ASC  " +
			" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		String sql2 = "select count(id) from sys_module  where"+where;
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}
}
